<template>
    <div class="comfirm flex-a flex-b">
        <div>合计：￥{{ total }}</div>
        <nut-button shape="square" type="info" @click="jumpTo">确认</nut-button>
    </div>
</template>

<script lang="ts">
import Taro, { useRouter } from '@tarojs/taro'
import mitt from "./../../../../utils/mitt";
import { reactive, toRefs, onMounted, onUnmounted } from 'vue';
import { postOrder } from '../../../../api/index'
export default {
    components: {

    },
    setup() {
        const data = reactive({
            total: 0,
            ids: []
        });
        onMounted(() => {
            mitt.on("total", (val) => {
                data.total = val
            })
            mitt.on("ids", (val) => {
                var r = val.filter((s) => {
                    return s && s.trim();
                });
                data.ids = r
            })
        });
        onUnmounted(() => {
            // Taro.redirectTo({ url: '/pages/cartpage/index' })
        });
        const jumpTo = () => {
            const router = useRouter();
            if (router.params.addressId) {
                postOrder({ cartItemIds: data.ids, addressId: router.params.addressId }).then(res => {
                    if (res.resultCode == 200) {
                        Taro.redirectTo({ url: '/packageA/pages/confirmPay/index?addressId=' + router.params.addressId + '&orderId=' + res.data });
                        Taro.setStorage({
                            key: "orderId",
                            data: res.data
                        })
                    }
                })
            } else {
                Taro.showToast({
                    title: '请选择收货地址',
                    icon: 'error',
                    duration: 2000
                })
            }
        }
        return {
            ...toRefs(data), jumpTo,
        }
    }
};
</script>


<style lang="scss" >
.comfirm {
    position: fixed;
    bottom: 0;
    left: 0;
    background: #fff;
    padding: 20rpx 40rpx;
    box-sizing: border-box;
    width: 100%;

    .nut-button {
        width: 200rpx;
        border-radius: 10rpx;
    }
}
</style>